import Ember from 'ember';

export function initialize(application) {
  // application.inject('route', 'foo', 'service:foo');
  Ember.Route.reopen({
    roles: 'ROLE_USER',
    beforeModel(transition) {
      var roles = this.get('roles'),
          userState = this.get('userState'),
          rn = this.get('routeName'),
          needAuthenticate = roles && !userState.hasAnyRole(roles);
      // console.log("from reopenjs:" + rn);
      if (rn === 'login') {
        return;
      }
      if (needAuthenticate) {
        var loginController = this.controllerFor('login');
        loginController.set('previousTransition', transition);
        this.transitionTo('login');
      }
    }
  });
}

export default {
  name: 'reopens',
  initialize: initialize
};
